/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __TEXTSTREAMFILE_H__
#define __TEXTSTREAMFILE_H__

#include <filesystem/File.h>
#include "TextStream.h"
#include <filesystem/String.hpp>

namespace liba
{
namespace textstream
{

template<class Sym>
class File : public TextOStream<Sym>
{
	filesystem::File rfw;
public:
	explicit File(const filesystem::String & filename, filesystem::File::Mode mode = filesystem::File::WRITE)
		:	rfw( filename, mode  )
	{}
	bool is_valid()const
	{
		return rfw.is_valid();
	}
	bool operator!()const
	{
		return !is_valid();
	}
	virtual void print(const Sym * begin, const Sym * end)
	{
		rfw.write( begin, (end - begin)*sizeof(Sym) );
	}
	virtual void flush()
	{
		rfw.flush();
	}
};

/** Specialization is required to print "Unicode header" to the file. */
template<>
class File< wchar_t > : public TextOStream<wchar_t>
{
	filesystem::File rfw;
public:
	explicit File(const filesystem::String & filename, filesystem::File::Mode mode = filesystem::File::WRITE)
		:	rfw( filename, mode  )
	{
		wchar_t header = 0xFEFF;
		print( &header, &header + 1 );
	}
	bool is_valid()const
	{
		return rfw.is_valid();
	}
	bool operator!()const
	{
		return !is_valid();
	}
	virtual void print(const wchar_t * begin, const wchar_t * end)
	{
		rfw.write( begin, (end - begin)*sizeof(wchar_t) );
	}
	virtual void flush()
	{
		rfw.flush();
	}
};

} // namespace textstream
} // namespace liba

using namespace liba;

#endif //__TEXTSTREAMFILE_H__